﻿Public Class frmTableSingleInsert

    Dim strNameForm As String = "Insert "
    Dim oTableSingleInfo As Data.TableSingleInfo
    Public mId As Integer

    Public Function Initialize(ByRef inTableSingleInfo As Data.TableSingleInfo) As Boolean
        Try
            strNameForm &= inTableSingleInfo.strNameForm
            Me.Text = strNameForm
            oTableSingleInfo = inTableSingleInfo

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "Commands"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInsert.Click
        Try
            If txtName.Text <> "" Then
                If Not Data.TableSingle.Exist(txtName.Text, -1, oTableSingleInfo.strNameTable) Then
                    Dim oTableSingle As New Data.TableSingle
                    oTableSingle.strNameTable = oTableSingleInfo.strNameTable
                    oTableSingle.mName = txtName.Text
                    mId = oTableSingle.Insert
                    If mId > 0 Then
                        Me.DialogResult = Windows.Forms.DialogResult.OK
                    Else
                        MsgBox("Impossible insert " & oTableSingleInfo.strNameElementComplete, vbExclamation, Me.Text)
                        Me.DialogResult = Windows.Forms.DialogResult.Cancel
                    End If
                    oTableSingle = Nothing
                Else
                    MsgBox("Value is already in database.", vbExclamation, Me.Text)
                    txtName.Focus()
                End If
            End If
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "cmdInsert_Click", ex)
        End Try
    End Sub

#End Region

#Region "Form events"

    Private Sub frmTableSingleInsertion_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class